package leetcode.code0313;

import java.util.PriorityQueue;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution2 implements IDebug {

	public int nthSuperUglyNumber(int n, int[] primes) {
		int[] ans = new int[n];
		ans[0] = 1;
		PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> a[0] * ans[a[1]] - b[0] * ans[b[1]]);
		for (int i = 0; i < primes.length; i++) {
			queue.add(new int[] { primes[i], 0 });
		}
		int p = 1;
		while (p < n) {
			int[] cur = queue.poll();
			while (cur[0] * ans[cur[1]] <= ans[p - 1]) {
				queue.add(new int[] { cur[0], cur[1] + 1 });
				cur = queue.poll();
			}
			ans[p] = cur[0] * ans[cur[1]];
			queue.add(new int[] { cur[0], cur[1] + 1 });
			p++;
		}
		return ans[n - 1];
	}

	@Override
	public void debug1() {
		HelpDebug.compare(32, this.nthSuperUglyNumber(12, HelpDebug.str2array3("[2,7,13,19]")));
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();

	}

}
